define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    $.widget('rk.colleagueCurtainCtrl', $.rk.oaCurtainCtrl, {
        _create: function () {
            var me = this;
            var elem = me.element;
            me._listeners();
            var html = require('oa/tmpl/colleague/tpl_colleague_curtain.tpl');
            elem.html(rk.templateText(html));
            me._initSearchCondition();
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            elem.on("click", '[act="close"]', function () {
                me.closeMe();
            });//colleagueSearchType
            me._searchListener();
            //me._row_click_listeners();
        },
        _searchListener:function(){
            var me = this;
            var elem = me.element;
            elem.on("click", "[act='colleagueSearchButton']", function(){
                var param = {pageNo:1,pageSize:20};
                var form = elem.find("[act='colleagueSearchForm']");
                var formData = form.serializeArray();
                //遍历formData 获取参数 判断departId
                $.each(formData, function (i, data) {
                    if(data.value) {
                        if (data.name === "departId") {
                            param["personalProfile." + data.name] = data.value;
                        } else {
                            param[data.name] = data.value;
                        }
                    }
                });
                me.loaddata("colleague",param);
            });
            elem.on("click", "[act='colleague_return_all_button']", function(){
                me.closeMe();
            });
        },
        /*_row_click_listeners:function(){
            //moreColleagueOperation
            var me = this;
            var elem = me.element;
            elem.on("click", "[act='moreColleagueOperation']", function(){
                var button = $(this);
                button.parent().find("[act='moreColleagueOperationMenu']").toggle();
            });
            elem.on("blur", "[act='moreColleagueOperation']", function(){
                var button = $(this);
                button.parent().find("[act='moreColleagueOperationMenu']").hide();
            });
            elem.on("mousedown", "[act='push_private_msg']", function(){
                var button = $(this);
                var uid = button.attr("uid");
                alert("发企业微信---uid="+uid)
            });
            elem.on("mousedown", "[act='push_task']", function(){
                var button = $(this);
                var uid = button.attr("uid");
                alert("建任务---uid="+uid)
            });
            elem.on("mousedown", "[act='push_schedule']", function(){
                var button = $(this);
                var uid = button.attr("uid");
                alert("建日程---uid="+uid)
            });
        },*/
        _init: function(){
            var me = this;
            var elem = me.element;
            elem.find('[act="beScroll"]').beScroll();
        },
        _initSearchCondition:function(){
            var me = this;
            var elem = me.element;
            var html = "";
            html += '<option value="">'+rk.i18n('COLLEAGUE_ALL_COLLEAGUE')+'</option>';
            html += '<option value="1" >'+rk.i18n('COLLEAGUE_THIS_MONTH_BIRTHDAY')+'</option>';
            html += '<option value="2" >'+rk.i18n('COLLEAGUE_THIS_MONTH_ENTRY')+'</option>';
            elem.find("[act='colleagueSearchType']").html(html);
            rk.post("/json/oa_colleague/initDepartList.action",{},function(json){
                if(json.status === 0){
                    //加载部门下拉树------------------------------------
                    if(!json.tenantHaveOrganizeFunction){
                        elem.find("[act='colleague_query_depart_condition']").hide();
                    }else{
                        //加载部门树
                        elem.find("[act='colleagueSearchDepart']").singletreeselector({
                            //nowrap:true,
                            width: '100%', //控件宽度
                            height: 28, //控件高度
                            clickSelector: elem.find("[act='colleagueSearchDepart']"),
                            menuWidth: 250,
                            source: '/json/oa_group/depart-tree.action?dimType=1',
                            onSuccess: function (o) {
                                //elem.find("[act='colleagueSearchDepart']").html(o.label);
                                elem.find("[act='colleagueSearchDepart']").val(o.label);
                                elem.find("[act='colleagueSearchDepartId']").val(o.value);
                            }
                        });
                    }
                }else{
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                }
            });
            elem.find("[act='colleagueSearchType']").selectmenu();
            //elem.find("[act='colleagueSearchDepart']").selectmenu();

        },
        loaddata:function(type,param){
            var me = this;
            var elem = me.element;
            if(type === "colleague"){
                if(!param){
                    param = {pageNo:1,pageSize:20};
                }
                elem.find("[act='colleague_query_table']").beLoading();
                elem.find('[act="colleague_curtain_page"]').hide();
                elem.find("[act='colleague_curtain_page_parent']").hide();
                rk.post("/json/oa_colleague/colleague-query-list.action",param,function(json){
                    if(json.status === 0){
                        if(json.colleagues && json.colleagues.length>0) {
                            var dataList = json.colleagues;
                            var temp = require('oa/tmpl/colleague/tpl_colleague_colleague_table_row.tpl');
                            //var html = rk.templateText(temp,{});
                            var listHtml = "";

                            $.each(dataList, function (i, dataJson) {
                                dataJson.currentUserId = json.currentUserId;
                                listHtml += rk.templateText(temp, dataJson);
                            });
                            elem.find("[act='colleague_query_table']").html(listHtml);
                            elem.find("[act='colleague_return_all']").show();
                            elem.find("[act='colleague_name_query_num']").html(json.dataCount);
                            elem.find("[act='colleague_curtain_page_parent']").show();
                            elem.find('[act="colleague_curtain_page"]').rkPaging({
                                pagenum: json.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.pageSize;
                                    me.loaddata(type, params)
                                }
                            });
                        }else{
                            elem.find("[act='colleague_return_all']").hide();
                            require.async(["oa/tmpl/colleague/tpl_colleague_none.tpl"], function (temp) {
                                var html = rk.templateText(temp, {text: rk.i18n('COLLEAGUE_NO_SUITABLE_COLLEAGUE_ERROR')});
                                elem.find("[act='colleague_query_table']").html(html);
                            });
                        }
                        elem.find("[act='colleague_table_follow']").beTip({dir:"left"});
                        elem.find("[act='moreColleagueOperation']").beTip({dir:"left"});
                    }else{
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                });
            }
        }
    });
});